var MapItem = (function () {
  var Instance = (function () {
    return {
      getGuardarButton : function () {
        return $( '.info-' + this.index + ' #guardar' );
      },
      getModificarButton : function () {
        return $( '.info-' + this.index + ' #modificar' );
      },
      initialize : function () {
        if (this.data) {
          this.getGuardarButton().attr('hidden', true);
          this.getModificarButton().removeAttr('hidden');
        } else {
          this.getGuardarButton().removeAttr('hidden');
          this.getModificarButton().attr('hidden', true);
        }
        
        if (!mapControllers[this.index].storeFunctionSettled) {
          console.log('setting up button!', this.getGuardarButton());
          this.getGuardarButton().click(
            (function (mapItem) {
              return function (e) {
                e.preventDefault();

                var data =
                    util.makeDataObject(mapItem.setOfNames, mapItem.index);
                console.log(data);
                mapItem.data = data;
                $.ajax({
                  url : mapItem.serverCall,
                  type : 'post',
                  data : data,
                  success : function (data) {
                    data = JSON.parse(data);
                    mapItem.onSuccess(data);
                    GoogleMap.resetMarkers();1
                  }
                });
              }
            })(this));
          mapControllers[this.index].storeFunctionSettled = true;
        }
        this.onCreate();
      }
    };
  })();

  return {
    create : function (index, name, setOfNames, serverCall,
                       onSuccess, onCreate) {
      var mapItem = {
        index : index,
        name : name,
        setOfNames : setOfNames,
        serverCall : serverCall,
        onSuccess : onSuccess,
        onCreate : onCreate
      };
      util.inheritFunctions(mapItem, Instance);

      this.collection.push(mapItem);
      return mapItem;
    },
    collection : []
  };
})();
